/*
 * Misc.cpp
 *
 * Copyright (c) 2009-2011 [BRN] aka. SWW13.
 *
 * This file is part of [BRN]Lib.
 *
 * [BRN]Lib is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * [BRN]Lib is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with [BRN]Lib.  If not, see <http ://www.gnu.org/licenses/>.
 */

#include "Misc.h"

namespace BRNlib
{
	namespace Tools
	{
		bool Misc::bCompare(const BYTE* pData, const BYTE* bMask, const char* szMask)
		{
			for(; *szMask; ++szMask,++pData,++bMask)
			{
				if(*szMask=='x' && *pData != *bMask)
					return 0;
			}

			return (*szMask) == NULL;
		}
		DWORD Misc::FindPattern(DWORD dwAddress, DWORD dwLen, BYTE *bMask, char * szMask, int codeOffset, BOOL extract)
		{
			for(DWORD i = 0; i < dwLen; i++)
			{
				if(bCompare((BYTE*)(dwAddress + i), bMask, szMask))
				{
					if(extract)
					{
						return *(DWORD*)(dwAddress + i + codeOffset);
					}
					else
					{
						return (DWORD)(dwAddress + i + codeOffset);
					}
				}
			}

			return NULL;
		}

		DWORD Misc::QuickChecksum(DWORD *pData, int size)
		{
			if(!pData) { return 0x0; }

			DWORD sum;
			DWORD tmp;
			sum = *pData;

			for(int i = 1; i < (size/4); i++)
			{
				tmp = pData[i];
				tmp = (DWORD)(sum >> 29) + tmp;
				tmp = (DWORD)(sum >> 17) + tmp;
				sum = (DWORD)(sum << 3)  ^ tmp;
			}

			return sum;
		}

		POINT Misc::GetScreenSize()
		{
			POINT ScreenSize;
			ScreenSize.x = GetSystemMetrics(SM_CXSCREEN);
			ScreenSize.y = GetSystemMetrics(SM_CYSCREEN);

			return ScreenSize;
		}

		POINT Misc::GetScreenCenter()
		{
			POINT ScreenCenter = GetScreenSize();
			ScreenCenter.x /= 2;
			ScreenCenter.y /= 2;

			return ScreenCenter;
		}

		char *Misc::Date()
		{
			time_t LongTime = time(0);
			tm NewTime;
			localtime_s(&NewTime, &LongTime);

			static char Date[11];
			strftime(Date, 11, "%d.%m.%Y", &NewTime);

			return Date;
		}
		char *Misc::Time()
		{
			time_t LongTime = time(0);
			tm NewTime;
			localtime_s(&NewTime, &LongTime);

			static char TimeText[9];
			strftime(TimeText, 9, "%H:%M:%S", &NewTime);

			return TimeText;
		}

		char *Misc::GetDllPath(HINSTANCE hInst)
		{
			static char DllDir[320];
			memset(DllDir, 0, 320); //leeren

			GetModuleFileNameA(hInst, DllDir, 320);
			
			for(int i = strlen(DllDir); i > 0; i--)
			{
				if(DllDir[i] == '\\')
				{
					DllDir[i+1] = 0;
					break;
				}
			}

			return DllDir;
		}
		char *Misc::GetDirectoryFile(char* Directory, char* FileName)
		{
			static char Path[320];
			
			strcpy_s(Path, Directory);
			strcat_s(Path, FileName);
			
			return Path;
		}

		RECT Misc::GetRect(POINT Pos, POINT Size)
		{
			RECT Rec;
			Rec.left = Pos.x;
			Rec.top = Pos.y;
			Rec.right = Pos.x + Size.x;
			Rec.bottom = Pos.y + Size.y;

			return Rec;
		}
	}
}